﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentApartMentManage
{
    public partial class ModifyAdmin : Form
    {
        public ModifyAdmin()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = string.Format("select * from 公寓管理员信息表 where 管理员ID='{0}'", textBox5.Text.Trim());
                dataGridView1.DataSource = sqlHelper.GetTable(sql);
                MessageBox.Show("查找成功!");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("查找失败!");
                return;
            }
        }

        private void ModifyAdmin_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = sqlHelper.GetTable("select * from 公寓管理员信息表");
        }

        void show()
        {
            textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
            textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
            textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
            if (dataGridView1.SelectedCells[3].Value.ToString() == "女")
            {
                radioButton2.Checked = true;
            }
            else
                radioButton1.Checked = true;
            textBox4.Text = dataGridView1.SelectedCells[4].Value.ToString();
            comboBox1.Text = dataGridView1.SelectedCells[5].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if(MessageBox.Show("你真的要修改该管理员的信息吗?","警告",MessageBoxButtons.OKCancel)==DialogResult.OK)
                {
                    string sql = string.Format("update 公寓管理员信息表 set 密码='{0}',姓名='{1}',性别='{2}',联系方式='{3}',职务='{4}' where 管理员ID='{5}'", textBox2.Text, textBox3.Text.Trim(), radioButton1.Checked ? "男" : "女", textBox4.Text.Trim(), comboBox1.Text.Trim(), dataGridView1.SelectedCells[0].Value.ToString());
                    //MessageBox.Show(sql);
                    int i = sqlHelper.Execute(sql);
                    if(i!=-1)
                    {
                        MessageBox.Show("修改管理员信息成功!");
                        dataGridView1.DataSource = sqlHelper.GetTable("select * from 公寓管理员信息表");
                    }else
                    {
                        MessageBox.Show("修改管理员信息失败!");
                    }

                }
            }catch(Exception ex)
            {
                MessageBox.Show("修改失败!");
                return;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            show();
        }
    }
}
